﻿using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ShootSearch.FileSystem;
using System.IO;
using ShootSearch.Util;

namespace ShootSearch.Tests.FileSystem
{
    [TestFixture]
    public class FileSystemTests
    {
        public static void Run()
        {
            FileSystemTests obj = new FileSystemTests();
            obj.FileEntitySerializeTest();
            obj.FileEntitySelfDeleteTest();
            obj.FileSystemTest();

            obj.TearDown();
        }

        private FileObject GetFileEntity()
        {
            FileObject ent = new FileObject();
            ent.Url = "http://www.shootsoft.net";
            ent.ContentType = "text/plain";
            ent.Encoding = Encoding.UTF8;
            ent.Content = Encoding.UTF8.GetBytes("Home page");
            ent.DateTime = DateTime.Now;
            ent.Deleted = false;
            return ent;
        }


        [Test]
        public void FileEntitySerializeTest()
        {
            FileObject ent = GetFileEntity();
            byte[] buff = ent.ToBytes();
            FileObject fe = new FileObject();
            fe.FromBytes(buff);
            Assert.AreEqual(ent.Content, fe.Content);

        }


        [Test]
        public void FileEntitySelfDeleteTest()
        {
            FileObject ent = GetFileEntity();
            ent.Deleted = true;
            byte[] buff = ent.ToBytes();
            FileObject fe = new FileObject();
            fe.FromBytes(buff);
            Assert.AreEqual(ent.Deleted, fe.Deleted);
        }

        [Test]
        public void FileSystemTest()
        {
            IFileSystem fileSystem = new ShootSearch.FileSystem.STFileSystem();
            fileSystem.CreateOpen(IO.StartupPath + "\\FileSystemTest\\");
            FileObject fe = GetFileEntity();            
            fileSystem.Write(fe);
            fe.Url = "http://www.microsoft.com";
            fe.Content = Encoding.UTF8.GetBytes("MS Home page");
            fileSystem.Write(fe);
            fe.Content = Encoding.UTF8.GetBytes("MS's Home page");
            fileSystem.Write(fe);
            fileSystem.Close();
            fileSystem.CreateOpen();
            FileObject feexpected = GetFileEntity();
            IFileObject feactual1 = fileSystem.Read(feexpected.Url);

            Assert.AreEqual(feexpected.Content, feactual1.Content);

 

            IFileObject feactual2 = fileSystem.Read(fe.Url);

            string s1 = fe.Encoding.GetString(fe.Content);
            string s2 = feactual2.Encoding.GetString(feactual2.Content);

            Assert.AreEqual(fe.Content, feactual2.Content);

            fileSystem.FSID = "newID";
            fileSystem.Write(fe);
            fileSystem.Write(feexpected);

            IFileObject feactual3 = fileSystem.Read(fe.Url);
            IFileObject feactual4 = fileSystem.Read(feexpected.Url);

            Assert.AreEqual(fe.Content, feactual3.Content);
            Assert.AreEqual(feexpected.Content, feactual4.Content);

            fileSystem.Close();

            List<string> list = fileSystem.ListAllFSIDs();

            Assert.AreEqual(list.Count, 2);

            fileSystem.CreateOpen();
            List<string> listUrls = fileSystem.ListFileNames();
            
            for (int i = 0; i < listUrls.Count; i++)
            {
                Console.WriteLine(listUrls[i]);
            }

            fileSystem.Close();

        }

        [TearDown]
        public void TearDown()
        {
            StartUp();
        }

        [SetUp]
        public void StartUp()
        { 
           if (Directory.Exists(IO.StartupPath + "\\FileSystemTest\\"))
            {
                Directory.Delete(IO.StartupPath + "\\FileSystemTest\\", true);
            }
        }
    }
}
